#include <vector>
#include <iostream>
#include <unordered_map>

using namespace std;

class Solution {
public:
    // 时间复杂度 O(N)，N 为 cpdomanis 的长度
    vector<string> subdomainVisits(vector<string>& cpdomains) {
        unordered_map<string, int> m;

        for (string &cpdomain : cpdomains) {
            int index = cpdomain.find(' ');
            int visitCount = stoi(cpdomain.substr(0, index));

            string domain = cpdomain.substr(index + 1); 

            // 提取各级子域名并且统计访问次数
            while (index > 0) {
                m[domain] += visitCount;

                index = domain.find('.');
                domain = domain.substr(index + 1);
            }
        }

        vector<string> ans;
        for (auto &it : m) {
            ans.emplace_back(to_string(it.second) + ' ' + it.first);
        }

        return ans;
    }
};

int main() {
  vector<string> cpdomains = {
      "9001 discuss.leetcode.com"
  };

  vector<string> result = Solution().subdomainVisits(cpdomains);

  cout << '[';

  for (int i = 0; i < result.size() - 1; i++) {
    cout << result[i] << ", ";
  }

  cout << result[result.size() - 1] << ']' << endl;

  return 0;
}